package com.zhiyou100.review.day021.mydatagram;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @packageName: TestOfDatagramSocket01
 * @className: com.zhiyou100.review.day021.mydatagram
 * @Description: TODO 接收方
 * @author: yang
 * @date: 7/2/20
 */
public class TestOfDatagramSocket01 {
    public static void main(String[] args) throws Exception{
        DatagramSocket datagramSocket = new DatagramSocket(10000);
        // 创建一个 DatagramSocket

        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
        // 接受数据

        datagramSocket.receive(datagramPacket);
        // 接受数据

        String s = new String(bytes, 0, datagramPacket.getLength());
        // datagramPacket.getLength() 接收到的有效的字节个数
        System.out.println("收到消息："+s);
        // 解析数据
        InetAddress localAddress = datagramSocket.getLocalAddress();

        datagramSocket.close();
        // 关闭

    }
}
